package com.hotelsystem.service.impl;

import com.hotelsystem.dao.OrderDetailMapper;
import com.hotelsystem.dao.OrdersMapper;
import com.hotelsystem.pojo.OrderDetail;
import com.hotelsystem.pojo.Orders;
import com.hotelsystem.service.OrderDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class OrderDetailServiceImpl implements OrderDetailService {

    @Resource(name = "orderDetailMapper")
    private OrderDetailMapper orderDetailMapper;

    @Resource(name = "ordersMapper")
    private OrdersMapper ordersMapper;

    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    @Override
    public List<OrderDetail> list(String ordersId) throws Exception {
        return orderDetailMapper.list(ordersId);
    }

    @Override
    public Integer insert(OrderDetail orderDetail) throws Exception {
        //拿到原来的订单信息，修改总金额
        Orders orders = ordersMapper.listByOrdersId(orderDetail.getOrders().getOrdersId()).get(0);
        orders.setTotalMoney(orders.getTotalMoney() + orderDetail.getGoodsPrice());
        ordersMapper.updateAll(orders);
        return orderDetailMapper.insert(orderDetail);
    }
}
